/*
 * utils.cpp
 *
 *  Created on: 10.06.2012
 *      Author: sebastian
 */

#include "utils.h"

#include <stdio.h>
#include <fstream>
#include <iostream>
#include <map>

using namespace std;


/**
 *
 * @param i the number to be converted
 * @return i as string
 * @deprecated TODO: remove from code.
 */
string intToStr(int i) {
	//get length of i....
	int c = 1;
	for (int a=myAbs(i); a/10.0 >= 1.0; a/=10)
		c++;
	char s[c];
	sprintf(s, "%i", i);
	string res = s;
	return res;
}

//ugly global:
map<string, string> cachedFiles;
std::string fileToStr(const std::string fileName, bool cache) {

	string result = "";

#ifdef DEBUG
	cache = false;
#endif
	if (cache) {
		//low: not tested yet, as only tested in debug mode
		//maybe the map must be initialized first.
		map<string, string>::iterator it = cachedFiles.find(fileName);
		if (it == cachedFiles.end()) { //load file in cache
			ifstream filestream;

			filestream.open(fileName.c_str(), ifstream::in);

			while (filestream.good()) {
				result += (char) filestream.get();
			}

			filestream.close();

			result[result.length()-1] = '\n';

			cachedFiles.insert(make_pair(fileName, result));

		} else result = it->second;
	} else {

		ifstream filestream;

		filestream.open(fileName.c_str(), ifstream::in);

		while (filestream.good()) {
			result += (char) filestream.get();
		}

		filestream.close();
		result[result.length()-1] = '\n'; //delete undefined symbol at the end :(
	}

	return result;
}

void mLog(std::string s, LogType l) {
	//REM: definition may not make use of predefined parameters as ...= LT_NORMAL
	std::cout << (l == 1 ? "ERROR: " : "") << s.c_str() << std::endl;
}

string getValue(const string &input, const string &paramName) {
	//cout << endl << paramName << endl;

	//TODO: requests from debian webbrowser seems to make problems whe parsing...dont know why :(

	unsigned int where = input.find(paramName);
	if (where == string::npos)
		return "";

	//find end
	unsigned int end = input.find('\n', where);
	unsigned int secondEnd = input.find('&', where); //26 = '&'
	unsigned int thirdEnd = input.find(' ', where);  //20 = ' '
	unsigned int fourthEnd = input.find(';', where);
	if (end == string::npos)
		end = secondEnd;
	else if ((secondEnd != string::npos) && (secondEnd < end))
		end = secondEnd;

	if ((thirdEnd != string::npos) && (thirdEnd < end))
		if (input[thirdEnd-1] == ':') //not the space after ":"
			thirdEnd = input.find(' ', where+thirdEnd + 1);

	if ((thirdEnd != string::npos) && (thirdEnd < end))
		end = thirdEnd;


	if ((fourthEnd != string::npos) && (fourthEnd < end))
		end = fourthEnd;

	if (end > input.length())
		end = input.length();

	//find separator ": " or "="
	unsigned int sep = input.find(": ", where);
	if ((sep == string::npos) || (sep > end))
		sep = input.find('=', where);
	else
		sep++; //saves the end of the separator

	/*cout << endl << "paramname:where:sep:end  " << paramName << ':' << where << ':' << sep
			<< ':' << end << endl;
	cout << "result: " << input.substr(sep + 1, end - (sep + 1) ) << '\n';*/

	return input.substr(sep + 1, end - (sep + 1) );
}
